package me.hacklh.velocity.test;


import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

public class Main {
    public static void main(String[] args) throws ResourceNotFoundException, ParseErrorException, Exception {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        ve.init();

        Template template_obj = ve.getTemplate("hello.vm");
        VelocityContext ctx = new VelocityContext();

        ctx.put("name", "velocity");
        ctx.put("date", (new Date()).toString());
        ctx.put("owner", "hacker");

        List temp = new ArrayList();
        temp.add("xx");
        temp.add("ee");
        temp.add("zzz");
        ctx.put("name_lists", temp);

        StringWriter sw = new StringWriter();

        template_obj.merge(ctx, sw);

        System.out.println(sw.toString());
    }
}